package com.hitqz.robot.biz.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.google.common.collect.Lists;
import com.hitqz.robot.api.business.enums.RobotDriverEnum;
import com.hitqz.robot.api.business.model.vo.robot.RobotDriverVersionVo;
import com.hitqz.robot.api.business.model.vo.robot.RobotVo;
import com.hitqz.robot.api.business.model.vo.robot.UpdateRobotParams;
import com.hitqz.robot.api.common.entity.Robot;
import com.hitqz.robot.api.common.entity.RobotMap;
import com.hitqz.robot.biz.service.RobotMapService;
import com.hitqz.robot.biz.service.RobotService;
import com.hitqz.robot.common.core.util.R;
import com.hitqz.robot.driver.parent.RobotDriverService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author xupkun
 * @date 2024/5/20
 */
@RestController
@AllArgsConstructor
@RequestMapping("/robot")
@Tag(name = "机器人管理")
public class RobotController {

    private final RobotService robotService;

    private final RobotMapService robotMapService;

    private final RobotDriverService robotDriverService;

    @GetMapping
    @Operation(summary = "获取当前部署的机器人数据")
    public R<RobotVo> details() {
        Robot robot = robotService.getDefaultRobot();
        RobotVo robotVo = new RobotVo();
        RobotMap robotMap = robotMapService.findByMapCode(robot.getMapCode());
        BeanUtil.copyProperties(robot, robotVo, true);
        robotVo.setMapName(Optional.ofNullable(robotMap).map(RobotMap::getMapName).orElse("未配置地图"));
        return R.ok(robotVo);
    }

    @PostMapping
    @Operation(summary = "更新机器人信息")
    public R<RobotVo> update(@RequestBody UpdateRobotParams params){
        Robot robot = robotService.getDefaultRobot();
        robot.setRobotSn(params.getRobotSn());
        robot.setRobotName(params.getRobotName());
        robot.setChargeThreshold(params.getChargeThreshold());
        robot.setServerAddress(params.getServerAddress());
        robot.setMapCode(params.getMapCode());
        robot.setIpAddress(params.getIpAddress());
        robot.setRobotLength(params.getRobotLength());
        robot.setRobotWidth(params.getRobotWidth());
        robot.setRobotAngleLength(params.getRobotAngleLength());
        robot.setWorkThreshold(params.getWorkThreshold());
        robot.setIsStandAlone(params.getIsStandAlone());
        if (robotService.updateById(robot)) {
            SpringUtil.getApplicationContext().publishEvent(robot);
        }
        return R.ok();
    }


    @GetMapping(value = "getDriverVersion")
    @Operation(summary = "获取驱动器版本列表")
    public R<List<RobotDriverVersionVo>> getDriverVersion() {
        List<RobotDriverVersionVo> vos = Lists.newArrayList();
        //机器人驱动器版本
        RobotDriverEnum driverVersion = robotDriverService.getDriverVersion();
        RobotDriverVersionVo vo = new RobotDriverVersionVo();
        vo.setDriverValue(driverVersion.getDriverValue());
        vo.setDriverName(driverVersion.getDriverName());
        vos.add(vo);
        return R.ok(vos);
    }


}
